package cn.itsub.boot.config;

import cn.itsub.boot.interceptor.CommonInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    CommonInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                //拦截所有请求
                .addPathPatterns("/**");
                //放行哪些请求
                /*.excludePathPatterns(
                        "/","/index","/static/**","/favicon.ico","/error",
                        "/login","/logout","/api/**","/druid/**",
                        "/swagger-ui.html","/swagger-resources/**",
                        "/v2/**","/webjars/**","/doc.html"
                );*/

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //设置放行哪些原始域   SpringBoot2.4.4下低版本使用.allowedOrigins("*")
                .allowedOriginPatterns("*")
                //放行哪些请求方式
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                //.allowedMethods("*") //或者放行全部
                //放行哪些原始请求头部信息
                .allowedHeaders("access-token","cdn-token","*")
                //暴露哪些原始请求头部信息
                .exposedHeaders("*");
    }


}


